let house_id = 0
let house_index = 0
Page({
  data: {
    isflag: false,
    dialogVisible: false,
    houseList: [],
  },
  async onShow() {
    this.gethouseList()
  },
  // 获取房屋列表
  async gethouseList() {
    const { code, data } = await wx.http({
      url: '/room',
    })
    if (code !== 10000) return wx.utils.toast('获取失败')
    this.setData({
      isflag: true,
      houseList: data || [],
    })
  },
  // 关闭弹窗
  swipeClose(ev) {
    console.log(ev)
    const { position, instance } = ev.detail

    if (position === 'right') {
      // 显示 Dialog 对话框
      this.setData({
        dialogVisible: true,
      })
      // 待删除的房屋id和索引
      house_id = ev.mark.id
      house_index = ev.mark.index

      // swiper-cell 滑块关闭
      instance.close()
    }
  },
  // 判断是否点击确定删除
  async dialogClose(e) {
    console.log(e.detail)
    e.detail === 'confirm' && this.deleteHouse()
  },
  // 删除业务
  async deleteHouse() {
    const { code } = await wx.http({
      url: `/room/${house_id}`,
      method: 'delete',
    })
    if (code !== 10000) {
      return wx.utils.toast('删除失败')
    } else {
      wx.utils.toast('删除成功')
      this.gethouseList()
    }
  },

  goDetail(ev) {
    console.log(ev)
    wx.navigateTo({
      url: '/house_pkg/pages/detail/index?id='+ev.mark.id
    })
  },

  addHouse() {
    wx.navigateTo({
      url: '/house_pkg/pages/locate/index',
    })
  },
})
